<?php

namespace hotel\common\models;

use Yii;

/**
 * This is the model class for table "activity".
 *
 * @property int $id
 * @property string $title
 * @property string $image_url 封面图
 * @property string $price 活动费用
 * @property string $time 活动时间
 * @property string $times
 * @property string $address 活动地点
 * @property string $open 活动开放
 * @property string $age 适合年龄
 * @property string $desc 活动介绍
 * @property string $end_time 截止日期
 * @property string $create_time
 */
class Activity extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'activity';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['price','number_of_people'], 'number'],
            [['desc'], 'string'],
            [['end_time', 'create_time'], 'safe'],
            [['title', 'time', 'address', 'open', 'age'], 'string', 'max' => 100],
            [['image_url'], 'string', 'max' => 500],
            [['content'], 'string']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'image_url' => 'Image Url',
            'price' => 'Price',
            'time' => 'Time',
            'address' => 'Address',
            'open' => 'Open',
            'age' => 'Age',
            'desc' => 'Desc',
            'end_time' => 'End Time',
            'create_time' => 'Create Time',
            'number_of_people' => 'Number Of People',
            'content' => 'Content',

        ];
    }

    /**
     * @param $arr
     * @return bool
     * 插入数据
     */
    public static function setData($arr){
        $model = new self();
        $model->setAttributes($arr);
        if($model->save()){
            return true;
        }
        return false;
    }


    /**
     * @param $arr
     * @return bool
     * 插入数据
     */
    public static function updateData($arr){
        $model = self::findOne($arr['id']);
        $model->setAttributes($arr);
        $model->create_time = date('Y-m-d H:i:s');
        if($model->save()){
            return true;
        }
        return false;
    }



    /**
     * @param $params
     * @return mixed
     */
    public static function lists($params){
        $page = 1;
        $limit = 10;
        extract($params);
        //分页
        $offset = ($page - 1) * $limit;
        $model = self::find();

        $total	= $model->count();
        $data = $model->offset($offset)->limit($limit)->asArray()->all();

        $res['list'] = $data;
        $res['total'] = $total;
        return $res;
    }
    public function getActivity()
    {
        return $this->hasMany(ActivityDate::className(), ['activity_id' => 'id'])->groupBy('date');
    }
}
